<?php
class Product extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}	
	public function add($intAdminId, $strName, $strDescription, $intCategoryId, $intSubCateId,
						$intPrice, $intPriceSale, $intDiscount, $intValue, $intHarvest,
						$strImage, $strImageExp, $intLimited, $intType, $intHot, $intDisOrder,
						$intQuantity) {
		try {	
			$this->prepare('call products_add(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, $strName, 			PDO::PARAM_STR, 255);
			$this->bindParam(3, $strDescription, 	PDO::PARAM_STR, 1000);
			$this->bindParam(4, $intCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(5, $intSubCateId,	 	PDO::PARAM_INT, 11);
			$this->bindParam(6, $intPrice, 			PDO::PARAM_INT, 11);
			$this->bindParam(7, $intPriceSale, 		PDO::PARAM_INT, 11);
			$this->bindParam(8, $intDiscount, 		PDO::PARAM_INT, 11);
			$this->bindParam(9, $intValue, 			PDO::PARAM_INT, 11);
			$this->bindParam(10, $intHarvest, 		PDO::PARAM_INT, 11);
			$this->bindParam(11, $strImage, 		PDO::PARAM_STR, 150);
			$this->bindParam(12, $strImageExp, 		PDO::PARAM_STR, 150);
			$this->bindParam(13, $intLimited, 		PDO::PARAM_INT, 6);
			$this->bindParam(14, $intType, 			PDO::PARAM_INT, 6);
			$this->bindParam(15, $intHot, 			PDO::PARAM_INT, 4);
			$this->bindParam(16, $intDisOrder, 		PDO::PARAM_INT, 11);
			$this->bindParam(17, $intQuantity, 		PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function update(	$intAdminId, $intProductId, $strName, $strDescription, $intCategoryId, 
							$intSubCateId, $intPrice, $intPriceSale, $intDiscount, $intValue,
							$intHarvest, $strImage, $strImageExp, $intLimited, $intType, $intHot, $intDisOrder,
							$intQuantity) {
		try {
			$this->prepare('call products_update(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, 		PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, 			PDO::PARAM_STR, 255);
			$this->bindParam(4, $strDescription, 	PDO::PARAM_STR, 1000);
			$this->bindParam(5, $intCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(6, $intSubCateId,	 	PDO::PARAM_INT, 11);
			$this->bindParam(7, $intPrice, 			PDO::PARAM_INT, 11);
			$this->bindParam(8, $intPriceSale, 		PDO::PARAM_INT, 11);
			$this->bindParam(9, $intDiscount, 		PDO::PARAM_INT, 11);
			$this->bindParam(10, $intValue, 		PDO::PARAM_INT, 11);
			$this->bindParam(11, $intHarvest, 		PDO::PARAM_INT, 11);
			$this->bindParam(12, $strImage, 		PDO::PARAM_STR, 150);
			$this->bindParam(13, $strImageExp, 		PDO::PARAM_STR, 150);
			$this->bindParam(14, $intLimited, 		PDO::PARAM_INT, 6);
			$this->bindParam(15, $intType, 			PDO::PARAM_INT, 6);
			$this->bindParam(16, $intHot, 			PDO::PARAM_INT, 4);
			$this->bindParam(17, $intDisOrder, 		PDO::PARAM_INT, 11);
			$this->bindParam(18, $intQuantity, 		PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateHot($intAdminId, $intProductId) {
		try {
			$this->prepare('call products_update_hot(?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::updateHot - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateStatus($intAdminId, $intProductId, $intStatus, $intPerm) {
		try {
			$this->prepare('call products_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $intStatus, 	PDO::PARAM_INT, 11);
			$this->bindParam(4, $intPerm, 		PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intAdminId, $intProductId) {
		try {
			$this->deleteImg($intProductId, 1);
			$this->deleteImg($intProductId, 2);
			$this->prepare('call products_delete(?,?)');
			$this->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intProductId, 	PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$this->clear->deleteProductId($intProductId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($intProductId) {
		try {		
			$strKey 	= BACKEND_PRODUCT_GET . $intProduct;
			$arrResult	= $this->cache->get($intProductId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call products_get(?)');
				$this->bindParam(1, $intProductId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, BACKEND_PRODUCT_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intAdType, $intType, $intCategoryId, $intSubCategoryId, $strKey, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call products_getall_admin(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(3, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intSubCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImg($intProductId, $intType=1) {
		
		$arrRow = $this->get($intProductId);
		if(isset($arrRow) && $arrRow) {
			if(isset($arrRow['image']) && $arrRow['image'] != '') {
				if(file_exists(DIR_PRODUCT . $arrRow['image']) && $intType == 1)
					@unlink(DIR_PRODUCT . $arrRow['image']);
				if(file_exists(DIR_PRODUCT . $arrRow['image_exp']) && $intType == 2)
					@unlink(DIR_PRODUCT . $arrRow['image_exp']);				
			}
		}		
	}
	/**
	* PRODUCT STT
	*/
	public function getAllStt($intAdType, $intType, $intCategoryId, $intSubCategoryId, $strKey, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call products_stt_getall(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(3, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intSubCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Product::getAllStt - ' .$e->getMessage());			
			return null;
		}
	}
}
?>